package com.sl.au.repository;

import com.sl.au.entity.PaymentRegist;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 开票历史
 * Created by RX on 2016-11-29.
 */
@Transactional(readOnly = true)
public interface PaymentRegistRepository extends JpaRepository<PaymentRegist, String> {

    /**
     * 获取开票历史数据
     * @param contractId
     * @param contractType
     * @return
     */
    @Query("select p from PaymentRegist p where p.contractId = ?1 and p.contractType=?2 and p.user.id=?3")
    Page<PaymentRegist> findByContractIdAndContractType(String contractId, String contractType,String userId,Pageable pageable);
    /**
     * 获取开票历史数据-财务
     * @param contractId
     * @param contractType
     * @return
     */
    @Query("select p from PaymentRegist p where p.contractId = ?1 and p.contractType=?2")
    Page<PaymentRegist> findByContractIdAndContractType(String contractId, String contractType,Pageable pageable);
    /**
     * 得到开票历史数据条数
     * @param contractId
     * @return
     */
    @Query(" select count(*) as num from PaymentRegist p where p.contractId = ?1")
    Long getPaymentRegistCount(String contractId);
    /**
     * 得到开票历史数据条数-财务
     * @param contractId
     * @return
     */
    @Query(" select count(*) as num from PaymentRegist p where p.contractId = ?1 and p.user.id=?2")
    Long getPaymentRegistCount(String contractId,String userId);
    /**
     * 得到修改开票之后的所有开票数据
     * @param contractId
     * @param createTime
     * @return
     */
    @Query("select p from PaymentRegist p where p.contractId = ?1 and p.createTime>=?2 order by p.createTime asc ")
    List<PaymentRegist> getChanges(String contractId, Date createTime);

    /**
     * 得到开票总额
     * @param contractId
     * @return
     */
    @Query("select p.paymentSum from PaymentRegist p where p.contractId = ?1")
    float[] getPaymentSum(String contractId);

    /**
     * 得到所有开票历史
     * @param contractId
     * @param contractType
     * @return
     */
    @Query("select p from PaymentRegist p where p.contractId = ?1 and p.contractType=?2 order by p.createTime asc")
    List<PaymentRegist> findByContractIdAndContractType(String contractId, String contractType);
}
